package com.anjho.dao;

import java.io.Serializable;
import java.util.List;

import javax.persistence.NoResultException;

import com.anjho.exceptions.InvalidLoginException;
import com.anjho.pojo.User;

public class DAOUser extends DAOGeneric implements Serializable {

	private static final long serialVersionUID = -4538459926877845629L;

	public DAOUser() {
		super();
	}

	@SuppressWarnings("unchecked")
	public List<User> getUserList() throws Exception {
		return (List<User>) super.getList("User.getUserList");
	}

	public User getUser(User user) throws Exception {
		User userReturn = null;
		try {
			userReturn = (User) entityManager
					.createNamedQuery("User.findByLoginAndPassword")
					.setParameter("login", user.getLogin())
					.setParameter("password", user.getPassword())
					.getSingleResult();
			if (userReturn == null) {
				throw new InvalidLoginException("Login e senha inválidos!");
			}
		} catch (InvalidLoginException e) {
			throw e;
		} catch (NoResultException e) {
			throw new InvalidLoginException("Login e senha inválidos!");
		} catch (Exception e) {
			throw e;
		}
		return userReturn;
	}
}